package com.example.testcontract.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.SectionIndexer;
import android.widget.TextView;

import com.example.testcontract.R;
import com.example.testcontract.bean.Contact;

import java.util.List;

/**
 * Created by admin on 2014/9/18.
 */
public class ContactAdapter extends ArrayAdapter<Contact> {
    /**
     * 需要渲染的item布局文件
     */
    private int resource;
    /**
     * 字母表分组工具
     */
    private SectionIndexer mIndexer;

    public ContactAdapter(Context context, int resource, List<Contact> objects) {
        super(context, resource, objects);
        this.resource = resource;
    }

    /**
    * 使用SectionIndexer的getSectionForPosition方法，通过当前的position值拿到了对应的section值，
    * 然后再反向通过刚刚拿到的section值，调用getPositionForSection方法，取回新的position值。
    * 如果当前的position值和新的position值是相等的，那么我们就可以认为当前position的项是某个分组下的第一个元素，
    * 我们应该将分组布局显示出来，而其它的情况就应该将分组布局隐藏。
    * */
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Contact contact = getItem(position);
        LinearLayout layout = null;
        if (convertView == null) {
            layout = (LinearLayout) LayoutInflater.from(getContext()).inflate(resource, null);
        } else {
            layout = (LinearLayout) convertView;
        }
        LinearLayout nameLayout= (LinearLayout) layout.findViewById(R.id.name_layout);
        final TextView name = (TextView) layout.findViewById(R.id.name);
        final TextView phoneNum = (TextView) layout.findViewById(R.id.phoneNum);
        ImageView iv_headImage = (ImageView) layout.findViewById(R.id.iv_headImage);
        LinearLayout sortKeyLayout = (LinearLayout) layout.findViewById(R.id.sort_key_layout);
        TextView sortKey = (TextView) layout.findViewById(R.id.sort_key);
        name.setText(contact.getName());
        phoneNum.setText(contact.getPhoneNumber());

        iv_headImage.setImageBitmap(contact.getImage());

        int section = mIndexer.getSectionForPosition(position);
        if (position == mIndexer.getPositionForSection(section)) {
            sortKey.setText(contact.getSortKey());
            sortKeyLayout.setVisibility(View.VISIBLE);
        } else {
            sortKeyLayout.setVisibility(View.GONE);
        }

//        nameLayout.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                Intent intent = new Intent();
//                intent.putExtra("name", name.getText());
//                intent.putExtra("phoneNum", phoneNum.getText());
//                intent.setClass(getContext(), ContactInformationActivity.class);
//                getContext().startActivity(intent);
//            }
//        });

        return layout;
    }

    public void setIndexer(SectionIndexer mIndexer) {
        this.mIndexer = mIndexer;
    }
}
